import Mock from "mockjs";
const formatDate = (date: Date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0"); // 月份补零
  const day = String(date.getDate()).padStart(2, "0"); // 日期补零
  const hours = String(date.getHours()).padStart(2, "0"); // 小时补零
  const minutes = String(date.getMinutes()).padStart(2, "0"); // 分钟补零
  const seconds = String(date.getSeconds()).padStart(2, "0"); // 秒数补零

  return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
};
const stateList = ["已上架", "待上架", "已下架"];
const shopTypeList = ["家用电器", "食品饮料", "其他"];
const imgList = [
  "https://pic.rmb.bdstatic.com/bjh/240424/news/be69897dbc8f50910da5116639fcc6fa8446.jpeg",
  "https://img2.baidu.com/it/u=4107181646,143803062&fm=253&fmt=auto?w=1280&h=800",
];
const total = 50;
const shop = Mock.mock({
  "list|50": [
    {
      "id|+1": 1,
      shopId: Mock.Random.guid(),
      name: "@cword(5, 10)",
      img: Mock.Random.pick(imgList),
      price: "@float(10, 100, 0, 0)",
      description: "@cparagraph(1, 3)",
      time: formatDate(new Date()),
      sales: "@float(10, 100, 0, 0)",
      inv: "@float(10, 100, 0, 0)",
      state: Mock.Random.pick(stateList),
      shopType: Mock.Random.pick(shopTypeList),
    },
  ],
});

// 获取商品列表
Mock.mock(/\/api\/shop.*/, "get", (options: any) => {
  const url = new URL(options.url, "http://xxxx.com");
  const currentPage = parseInt(url.searchParams.get("currentPage") || "1");
  const pageSize = parseInt(url.searchParams.get("pageSize") || "10");

  const start = (currentPage - 1) * pageSize;
  const end = start + pageSize;

  return {
    code: 200,
    message: "success",
    data: {
      list: shop.list.slice(start, end),
      total,
      pageSize,
      currentPage,
    },
  };
});

// 添加商品
Mock.mock("/api/shop/add", "post", (options) => {
  const body = JSON.parse(options.body);
  if (body) {
    shop.shopId = Mock.Random.guid();
    shop.list.push(body);
    return {
      code: 200,
      message: "添加成功",
    };
  } else {
    return {
      code: 400,
      message: "添加失败",
    };
  }
});

// 修改文件上传接口
Mock.mock("/api/upload/image", "post", () => {
  return {
    code: 200,
    message: "success",
    data: imgList,
  };
});

//批量删除
Mock.mock("/api/shop/AllDelete", "delete", (options) => {
  const { ids } = JSON.parse(options.body);
  if (ids && ids.length > 0) {
    // 过滤掉要删除的数据
    shop.list = shop.list.filter((item: any) => {
      return !ids.some((selected: any) => {
        selected.id === item.id;
      });
    });
    return {
      code: 200,
      message: "删除成功",
    };
  }
  return {
    code: 400,
    message: "删除失败",
  };
});

// 更新数据
Mock.mock("/api/shop/put", "put", (options) => {
  const { id, data } = JSON.parse(options.body);
  if (id) {
    const index = shop.list.findIndex((item: any) => item.id === id);
    if (index > -1) {
      shop.list[index] = {
        ...shop.list[index],
        ...data,
        time: formatDate(new Date()),
      };
      return {
        data: {
          code: 200,
          message: "更新成功",
        },
      };
    }
  }
  return {
    code: 400,
    message: "更新失败",
  };
});
// 删除单行数据
Mock.mock("/api/shop/delete", "delete", (options) => {
  const { ids } = JSON.parse(options.body);
  if (ids) {
    const index = shop.list.findIndex((item: any) => item.id === ids);
    if (index > -1) {
      shop.list.splice(index, 1);
      return {
        data: {
          code: 200,
          message: "删除成功",
        },
      };
    }
  }
  return {
    data: {
      code: 400,
      message: "删除失败",
    },
  };
});

// 搜索商品
Mock.mock(/\/api\/shop\/search.*/, "get", (options: any) => {
  const url = new URL(options.url, "http://xxxx.com");
  const keyword = url.searchParams.get("keyword") || "";
  const currentPage = parseInt(url.searchParams.get("currentPage") || "1");
  const pageSize = parseInt(url.searchParams.get("pageSize") || "10");

  // 过滤匹配的商品
  const filteredList = shop.list.filter(
    (item: any) =>
      item.name.toLowerCase().includes(keyword.toLowerCase()) ||
      item.shopType.toLowerCase().includes(keyword.toLowerCase())
  );

  const start = (currentPage - 1) * pageSize;
  const end = start + pageSize;

  return {
    code: 200,
    message: "success",
    data: {
      list: filteredList.slice(start, end),
      total: filteredList.length,
      pageSize,
      currentPage,
    },
  };
});

export default shop;
